<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class IdNumber implements Rule
{
    public function __construct(public string $message = '请输入正确的身份证号')
    {
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value): bool
    {
        $preg = "/^\d{6}(19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}[\dX]$/";
        return (bool)preg_match($preg, $value);
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message(): string
    {
        return $this->message;
    }
}
